package cn.schoolwow.quickdao.module.database.ddl.query.flow.index.getEntityIndexFlow;

import cn.schoolwow.quickdao.annotation.IndexType;
import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.database.ddl.IndexField;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.module.database.parent.domain.ResultSetConsumer;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteQueryConnectionFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.sql.ResultSet;
import java.util.Arrays;

public class GetEntityIndexByMySQLFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);
        Entity entity = (Entity) flowContext.checkData("entity");
        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("sql", "select table_name, index_name, non_unique, column_name, index_type, index_comment from information_schema.`statistics` where table_schema = '" + quickDAOConfig.databaseContext.databaseName + "' and table_name = ?;")
                .putTemporaryData("parameters", Arrays.asList(entity.tableName))
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        while (resultSet.next()) {
                            String indexName = resultSet.getString("index_name");
                            if("PRIMARY".equalsIgnoreCase(indexName)){
                                continue;
                            }
                            IndexField existIndexField = null;
                            for (IndexField indexField : entity.indexFieldList) {
                                if (indexField.indexName.equals(indexName)) {
                                    existIndexField = indexField;
                                    break;
                                }
                            }
                            if (null == existIndexField) {
                                IndexField indexField = new IndexField();
                                indexField.tableName = resultSet.getString("table_name");
                                indexField.indexType = resultSet.getInt("non_unique") == 0 ? IndexType.UNIQUE : IndexType.NORMAL;
                                if ("FULLTEXT".equals(resultSet.getString("index_type"))) {
                                    indexField.indexType = IndexType.FULLTEXT;
                                }
                                indexField.indexName = resultSet.getString("index_name");
                                indexField.columns.add(resultSet.getString("column_name"));
                                indexField.using = resultSet.getString("index_type");
                                indexField.comment = resultSet.getString("index_comment");
                                entity.indexFieldList.add(indexField);
                            } else {
                                existIndexField.columns.add(resultSet.getString("column_name"));
                            }
                        }
                    }
                })
                .execute();
    }

    @Override
    public String name() {
        return "获取数据库索引信息-MySQL";
    }
}
